In the previous article, I wrote about how to set up ndk R7 In ubuntu. This article implements my first JNI example. The Android SDK API version is 4.0.3 and the ndk version is R7.
1. Create an android project.
Open eclipse, execute file-> New-> Project, select Android-> Android project, and fill in the project name (my project name is myjnitest ), package name (for exa
JNI is part of the JDK and is used to provide Java with a local code interface. Programs written with JNI can ensure that your code is fully ported to all platforms. JNI enables Java code that runs on a JVM virtual machine to manipulate applications and libraries written in other languages, such as C + + and assembly language. In addition, some of the APIs provid
,jni_version_1_4)!=JNI_OK){return-1;}Jclass jthiz = Env->findclass (ClassPath);if (Env->registernatives (Jthiz,methods,sizeof (methods)/sizeof (Methods[0])) {return-1;}return jni_version_1_4;}As can be seen from the above code, the JNI layer through the Pthread library to complete the creation of threads, it is important to note that the JNI layer of the thread, must be through the global JAVAVM to get to t
JNI example
1. Introduction to JNI
JNI: Java Native Interface is a common interface provided by the Java language for interaction between Java code and localized code. Localized Code refers to machine-related Binary Code directly compiled, rather than intermediate code such as Java bytecode. In Windows, executable file
.
The ibm xl c/C ++ compiler can be™The processor generates extremely optimized binary code, which can significantly improve the performance of all C/C ++ code in most cases. The ibm xl c/C ++ compiler for Linux is well compatible with gnu c/C ++ and contains gxl * compiler variables, you can mark the gnu c/C ++ compiler as a corresponding command for the ibm xl c/C ++ compiler and then call it. When the ibm xl c/C ++ compiler is used, this significantly simplifies the porting of Makefile files
tool to get symbols from. In a nutshell, with the sign and debug information under Obj, Lib is a dynamic link library file that is stripped of these huge information and installed in On Android devices, only the. So file in the Libs directory is required.5, when the completion of this step, if we do not need to recompile later, we can directly delete the JNI directory and the obj directoryIv. load the. So class library in Java code, and then you can
Switch the terminal to the lt; sdk gt;/tools/directory and run the Android command to open AVDManager. However, if you do not add lt; sdk gt;/tools to your environment variable, enter the android press enter and the terminal will only prompt you for android: cannot find
Terminal Switch
Run the Android command in the/tools/directory to open AVD Manager. However, if you do not add
/Tools to your environment variables, enter android press enter and the terminal will only prompt you
Transferred from: http://blog.chinaunix.net/uid-20180960-id-1972669.htmlLinux platform Java call so library-jni use example 2010-08-11 22:171. Ensure that the GCC compiler is installed2. Write the Hellojni.java code and declare the functions that need to be implemented in C with native.If the source program is included in the package, the same folder structure should be created, such as/home/swan/test/net/w
To continue with the last blog post Eclipse to build the JNI development environment, now we analyze from the code point of view, C and Java mixed programming functions can be implemented.
With the Javah command, when you compile the. h header file, there will be at least two arguments for each function. JNIEnv and Jclass/jobject. wherein, when the native method is a static method (class method), the second parameter is Jclass, and when the native met
First, write a Java file that contains the native method public class Myjni {public native void display (); Public native double sum (double x, double y); public static void Main (string[] args) { }}Next, compile this Java file into an. h fileJavah Myjni After executing this command, a MyJni.h file is produced in the directory Third, write a. c file that introduces just the header file #include D. Compile the. c file into. O, and then convert to. so or. dll filesGcc-fpic-d_ree
Clscpp. Selecting DLLs at the application type of the wizardCopy the jni_md.h from the files jni.h and Win32 folders in the Include folder of your machine's JDK directory to the root of the Clscpp project and add them to the project.Here my copy has a problem, cause I can't find the file, I was newly created the file and then the code copied the pastNew source file Cls.cpp" jni.h " "Cls.h" void jnicall java_cls_clear (jnienv *, Jobject) { system ("Cls "); // In C + +, you can invoke the
This chapter uses a simple example to illustrate how to use JNI. We write a Java program and use it to call a C function to print "Hello World !".
2.1 Overview
Figure 2.1 demonstrates how to use a Java program to call the C function to print "Hello World !". This process includes the following steps:
1. Create a class (helloworld. Java) to declare the local method.
2. Use javac to compile the source
(msg.what)
{
Case 100://UPDATE_UI:
{
String[] Arrstrings = (string[]) msg.obj;
if (arrstrings[0].equals ("I ' m a Titile"))
{
TestJni.this.myView.setText ("C + + change Java editview string test!");
}
}
Break
Default
Break
}
}
};
Add: Here are two alternate functions that change the state of the input box
Myview.setbackgroundcolor (color.transparent)//change background color transparent
Framelayout.removeview (MyView);//Remove control
C + + Partial code
Step one: Create
Recently the project iteration several versions, at present relatively easy, although the project is idle, but the person cannot be too idle, otherwise is obsolete. A journey of a few, quantitative changes caused qualitative change, learning without thinking is not .... All right, all right. Can't make up, let us go straight to the point, or say a word, welcome everyone to exchange learning, common progress, welcome to throw bricks, even if you throw me do not answerFirst, build a new Android pr
1. Write a Java file in which to declare the native method and load the dynamic link library with a static statement block, the example Prompt.java code is as follows:
Class Prompt {
private native string Getline (string Prompt);
public static void Main (String args[]) {
Prompt p = new Prompt ();
String input = P.getline ("Type a Line:");
System.out.println ("User typed:" + input);
}
static {
system.loadlibrary ("Prom
A Java
Program Call the inherent method,
1. The first step is to write the Java Code :Class showmsgbox {Public static void main (string [] ARGs ){Showmsgbox APP = new showmsgbox ();App. showmessage ("generated with JNI ");}Private native void showmessage (string MSG );Static {System. loadlibrary ("msgimpl ");}}Behind the inherent method Declaration, there is a static code block that calls system. loadlibrary () (it can be called at any time, but i
Tested on centos 5.8
Step 1: declare a native function in the Java source file
public class AddMe{ static { System.loadLibrary("hello"); } public native void sum(int a, int b); public static void main(String[] args){ AddMe am = new AddMe(); am.sum(2, 5); }}
Step 2: Compile the Java class
javac AddMe.java
Step 3: generate the C header file
javah -jni AddMe
You will fin
Jni will be used when writing android programs. The following code is about C implementation. For environment configuration, please refer to my other blogs. If you don't talk about it, go directly to the code, almost every sentence in the code will be explained, which is absolutely understandable.
# Include "com_ndk_test_JniClient.h" # include
package com.ndk.test;public class JniClient {static {System.loadLibrary("TestNdk");}private JniClient() {}
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.